#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time      :2022/5/20 10:57
# @Author    :cjw
import logging
from time import sleep

from selenium.webdriver.common.by import By

from web_wework.page.base_page import BasePage


class AddMemberPage(BasePage):
	"""添加用户页面"""

	__INPUT_USERNAME = (By.ID, 'username')  # “用户名”输入框
	__USERNAME_TIPS = (By.CSS_SELECTOR, '#username+div')  # 输入用户名错误提示信息
	__INPUT_ACCTID = (By.ID, 'memberAdd_acctid')  # “acctid”输入框
	__ACCID_TIPS = (By.CSS_SELECTOR, '#memberAdd_acctid+div')  # 账号输入错误提示信息
	__INPUT_PHONE = (By.ID, 'memberAdd_phone')  # “手机号”输入框
	__PHONE_TIPS = (By.CSS_SELECTOR, '.ww_telInput+div')  # 手机号输入错误提示信息
	__RADIO_SEND_INVITE = (By.NAME, 'sendInvite')  # 单选按钮，取消短信发送企业邀请
	__BTN_SAVE = (By.XPATH, '//a[text()="保存"]')  # “保存”按钮

	def add_member(self, username: str, acctid: str, phone: str):
		"""
		添加成员
		:param username: 用户名
		:param acctid: 账号
		:param phone: 手机号
		:return:
		"""
		from web_wework.page.contact_page import ContactPage
		self._add_member(username, acctid, phone)
		return ContactPage(self.driver)

	def add_member_fail(self, username: str, acctid: str, phone: str, error_type: str):
		"""
		添加成员失败
		:param username:
		:param acctid:
		:param phone:
		:param error_type: 输入错误的内容类型
		:return:
		"""
		self._add_member(username, acctid, phone)
		# 等待3秒再获取错误信息
		sleep(1)
		if error_type.lower() == 'username':
			tip_text = self.find(self.__USERNAME_TIPS).text
		elif error_type.lower() == 'acctid':
			tip_text = self.find(self.__ACCID_TIPS).text
		else:
			tip_text = self.find(self.__PHONE_TIPS).text
		logging.info(f'获取的错误提示信息：{tip_text}')
		return tip_text

	def _add_member(self, username: str, acctid: str, phone: str):
		"""添加成员"""
		# 输入用户等信息并保存
		logging.info(f'填写成员信息，用户名：{username}, 账号: {acctid}, 手机号：{phone}')
		self.send_keys(self.__INPUT_USERNAME, username)
		self.send_keys(self.__INPUT_ACCTID, acctid)
		self.send_keys(self.__INPUT_PHONE, phone)
		self.click(self.__RADIO_SEND_INVITE)
		self.click(self.__BTN_SAVE)
